﻿using System;
using Close2Poco.Tests.TestEntities;
using NUnit.Framework;

namespace Close2Poco.Tests
{
    /// <summary>
    /// Tests to ensure that bi-directional relationships are being maintained - i.e when we set one end, the right thing happens at the other end
    /// </summary>
    [TestFixture]
    public class BidirectionalRelationships
    {
        [Test]
        public void CanAddDetailToMaster()
        {
            Customer cust = new Customer { CustomerID = "CO", CompanyName = "Co Name" };
            Order ord = new Order { OrderDate = DateTime.Now };

            cust.Orders.Add(ord);

            Assert.AreSame (cust,            ord.Customer);   // order's customer was automatically assigned
            Assert.AreEqual(cust.CustomerID, ord.CustomerID); // order's customer ID was automatically assigned
            // TODO: test key assignment with multi-part keys (if/when we actually support them)

            Assert.IsTrue  (cust.Orders.Contains(ord));       // and the order is indeed in the list that we put it in
        }

        [Test]
        public void CanSetMasterOnDetail()
        {
            Customer cust = new Customer { CustomerID = "CO", CompanyName = "Co Name" };
            Order ord = new Order { OrderDate = DateTime.Now };

            ord.Customer = cust;

            Assert.IsTrue(cust.Orders.Contains(ord));         // it was automatically added to the list
            Assert.AreEqual(cust.CustomerID, ord.CustomerID); // order's customer ID was automatically assigned
        }

        [Test]
        public void CanSwitchDetailBetweenMasters()
        {
            Customer cust1 = new Customer { CustomerID = "CO1", CompanyName = "Co Name 1" };
            Customer cust2 = new Customer { CustomerID = "CO2", CompanyName = "Co Name 2" };
            Order ord = new Order { OrderDate = DateTime.Now };

            ord.Customer = cust1;
            ord.Customer = cust2;

            Assert.IsFalse(cust1.Orders.Contains(ord));       // it was automatically removed from the old one
            Assert.IsTrue(cust2.Orders.Contains(ord));        // and added to the new
            Assert.AreEqual(cust2.CustomerID, ord.CustomerID); // order's customer ID was is the new one
        }

        [Test]
        public void CanClearMasterFromDetail()
        {
            Customer cust = new Customer { CustomerID = "CO", CompanyName = "Co Name" };
            Order ord = new Order { OrderDate = DateTime.Now };

            ord.Customer = cust;
            ord.Customer = null;  

            Assert.IsFalse(cust.Orders.Contains(ord));         // it was automatically removed from the list
            Assert.AreEqual(null, ord.CustomerID);             // order's customer ID was automatically cleared
        }

        //TODO: add tests with retireval from DB, since the logic that looks to see if there is a loaded or assigned  value depends on that
    }
}
